package com.example.easycharge.repository;

import com.example.easycharge.entity.Vehicle;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository
public interface VehicleRepository extends JpaRepository<Vehicle, Long> {
    Vehicle findVehicleByVehicleId(long vehicleId);

    @Modifying
    @Transactional
    @Query(value = "update Vehicle vehicle set vehicle.charging=:isCharging where vehicle.vehicleId=:vehicleId")
    void setVehicleState(boolean isCharging, long vehicleId);

    int countVehicleByVehicleIdAndCustomerId(long vehicleId, long customerId);

    @Query(value = "select vehicleId from Vehicle where customerId=:customerId and charging = true")
    List<Long> getChargingVehicle(long customerId);

    @Modifying
    @Transactional
    @Query(value = "update Vehicle vehicle set vehicle.charging=false")
    void initQueue();

}
